package com.faasadmin.faas.modules.admin.admin.controller.system.tenant;

import com.faasadmin.faas.business.core.support.controller.BaseController;
import com.faasadmin.faas.services.tenant.convert.config.TenantConfigConvert;
import com.faasadmin.faas.services.tenant.dal.dataobject.config.TenantConfigDO;
import com.faasadmin.faas.services.tenant.service.config.TenantConfigService;
import com.faasadmin.faas.services.tenant.vo.config.*;
import com.faasadmin.framework.common.pojo.CommonResult;
import com.faasadmin.framework.common.pojo.PageResult;
import com.faasadmin.framework.excel.core.util.ExcelUtils;
import com.faasadmin.framework.operatelog.core.annotations.OperateLog;
import com.faasadmin.framework.security.core.util.SecurityFrameworkUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.faasadmin.framework.common.enums.OperateTypeEnum.EXPORT;
import static com.faasadmin.framework.common.pojo.CommonResult.success;

@Tag(name = "租户配置")
@RestController
@RequestMapping("/system/tenant-config")
@Validated
public class TenantConfigController extends BaseController {

    @Resource
    private TenantConfigService tenantConfigService;

    @PostMapping("/create-json")
    @Operation(summary = "创建租户配置")
    @PreAuthorize("@ss.hasPermission('system:tenant-config:create')")
    public CommonResult<Long> createTenantJsonConfig(@Valid @RequestBody TenantJsonConfigCreateReqVO createReqVO) {
        setAppInfo(createReqVO);
        return success(tenantConfigService.createTenantConfig(createReqVO));
    }

    @PostMapping("/create")
    @Operation(summary = "创建租户配置")
    @PreAuthorize("@ss.hasPermission('system:tenant-config:create')")
    public CommonResult<Long> createTenantConfig(@Valid @RequestBody TenantConfigCreateReqVO createReqVO) {
        setAppInfo(createReqVO);
        return success(tenantConfigService.createTenantConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新租户配置")
    @PreAuthorize("@ss.hasPermission('system:tenant-config:update')")
    public CommonResult<Boolean> updateTenantConfig(@Valid @RequestBody TenantConfigUpdateReqVO updateReqVO) {
        setAppInfo(updateReqVO);
        tenantConfigService.updateTenantConfig(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-json")
    @Operation(summary = "更新租户配置")
    @PreAuthorize("@ss.hasPermission('system:tenant-config:update')")
    public CommonResult<Boolean> updateTenantJsonConfig(@Valid @RequestBody TenantJsonConfigUpdateReqVO updateReqVO) {
        setAppInfo(updateReqVO);
        tenantConfigService.updateTenantJsonConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除租户配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:tenant-config:delete')")
    public CommonResult<Boolean> deleteTenantConfig(@RequestParam("id") Long id) {
        tenantConfigService.deleteTenantConfig(id);
        return success(true);
    }

    @GetMapping("/get-key")
    @Operation(summary = "获得租户配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:tenant-config:query')")
    public CommonResult<TenantConfigRespVO> getTenantConfigByKey(@RequestParam("key") String key) {
        TenantConfigDO tenantConfig = tenantConfigService.getTenantConfigByKey(SecurityFrameworkUtils.getTenantId(), getHeaderAppShort(), key);
        return success(TenantConfigConvert.INSTANCE.convert(tenantConfig));
    }

    @GetMapping("/get-json-key")
    @Operation(summary = "获得租户配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:tenant-config:query')")
    public CommonResult<TenantJsonConfigRespVO> getTenantJsonConfigByKey(@RequestParam("key") String key) {
        TenantConfigDO tenantConfig = tenantConfigService.getTenantConfigByKey(SecurityFrameworkUtils.getTenantId(), getHeaderAppShort(), key);
        return success(TenantConfigConvert.INSTANCE.convertToJsonConfig(tenantConfig));
    }

    @GetMapping("/get")
    @Operation(summary = "获得租户配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:tenant-config:query')")
    public CommonResult<TenantConfigRespVO> getTenantConfig(@RequestParam("id") Long id) {
        TenantConfigDO tenantConfig = tenantConfigService.getTenantConfig(id);
        return success(TenantConfigConvert.INSTANCE.convert(tenantConfig));
    }

    @GetMapping("/list")
    @Operation(summary = "获得租户配置列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('system:tenant-config:query')")
    public CommonResult<List<TenantConfigRespVO>> getTenantConfigList(@RequestParam("ids") Collection<Long> ids) {
        List<TenantConfigDO> list = tenantConfigService.getTenantConfigList(ids);
        return success(TenantConfigConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得租户配置分页")
    @PreAuthorize("@ss.hasPermission('system:tenant-config:query')")
    public CommonResult<PageResult<TenantConfigRespVO>> getTenantConfigPage(@Valid TenantConfigPageReqVO pageVO) {
        PageResult<TenantConfigDO> pageResult = tenantConfigService.getTenantConfigPage(pageVO);
        return success(TenantConfigConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出租户配置 Excel")
    @PreAuthorize("@ss.hasPermission('system:tenant-config:export')")
    @OperateLog(type = EXPORT)
    public void exportTenantConfigExcel(@Valid TenantConfigExportReqVO exportReqVO,
                                        HttpServletResponse response) throws IOException {
        List<TenantConfigDO> list = tenantConfigService.getTenantConfigList(exportReqVO);
        // 导出 Excel
        List<TenantConfigExcelVO> datas = TenantConfigConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "租户配置.xls", "数据", TenantConfigExcelVO.class, datas);
    }

}
